﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;

namespace Fuse8.ContentManagementFramework.SystemFacade.Extensions
{
    public static class EnumExtensions
    {
        public static string GetDescription(this Enum value)
        {
            // Get fieldinfo for this type
            var fieldInfo = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes = null;

            if (fieldInfo != null)
            {
                // Get the description attributes
                attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
            }

            return (attributes != null) && (attributes.Length > 0) ?
                attributes[0].Description : 
                string.Empty;
        }
    }
}
